#include "parser.h"
#include <stdio.h>
#include <stdlib.h>
#include "debug.h"


/*
 * fields :
 * 1 -> submit_time : job will be submit at time t
 * 2 -> run_time : time needed for the job to be done
 *      (it will be soon amount of computation)
 * 3 -> input_size : amount of data to transfert before treatment
 * 4 -> ouput_size : amount of data to transfert after
 * 5 -> requested time : define the end size of a reservation
 * 6 -> nb_procs : nb procs requested for a task   
 * 7 -> priority : priority of the task
 */




void parse(const char * wld_file, const char * name, std::list<Job>& theResultList) {
    int i = 1;
    FILE * f = NULL;
    char buf[2048];
    
    printf ("parsing %s\n", wld_file);
    if ((f = fopen(wld_file, "r")) == NULL) {
		fprintf (stderr, "%s:line %d, function %s, fopen failed : %s \n",\
			 __FILE__, __LINE__, __func__, wld_file);
		return;
    }
    
    while (fgets(buf,512,f) != NULL) {
		/* Comments */
		if (buf[0] != ';') {
			Job job;
			
			job.start_time = 0.0;
			/* Need to ajsust and keep only the usefull data */
			sscanf(buf,"%lf %lf %lf %lf %lf %d %d", &(job.submit_time),\
			   &(job.run_time), &(job.input_size), &(job.output_size),\
			   &(job.requested_time), &(job.nb_procs), &(job.priority));
			job.state = WAITING;
	  
		    sprintf(job.name, "%s%d", name, i++);
			theResultList.push_back(job);
			
			//printf("%s %lf %lf %lf %lf %d %s\n", job.name, job.submit_time,
			// 	job.run_time, job.input_size, job.requested_time, job.nb_procs, job.service);
		}
    }
    fclose(f);
    
    //Job& last = theResultList.back();
    //last.run_time = 100000;
    
    //std::cout << theResultList << "\n";
    
    printf("%s loaded\n", wld_file);
    
}
